package javase.CollectionAndMap.list;

import java.util.LinkedList;

/**
 * LinkedList实现类
 * 1.基于链表的数据结构，双向链表，在内存空间是不连续的
 * 2.查询慢，增删块
 * 3.线程不安全
 *
 * 使用场景：
 * 1.数据库连接池
 * 2.浏览足迹
 * 3.操作头尾比较频繁、查询少但是增删多的场景，优先选择LinkedList
 */
public class LinkedListDemo {

    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("111");
        list.add("222");
        list.add("333");
        list.add("444");
        System.out.println("list = "+list);

        System.out.println("================= 添加 =================");
        //public void addFirst(E e)  没有返回值
        list.addFirst("abc");
        list.addLast("add");
        System.out.println("addFirst&addLast添加后  list = "+list);

        //public boolean offerFirst(E e)  返回添加是否成功的boolean值
        System.out.println("offerFirst添加是否成功："+list.offerFirst("ddd"));
        System.out.println("offerFirst添加后  list = "+list);


        System.out.println("================= 获取 =================");
        System.out.println("getFirst = "+list.getFirst());
        System.out.println("getLast = "+list.getLast());

        //public E element()   获取第一项元素，直接就是getFirst()
        System.out.println("element = "+list.element());

        //public E peekFirst() 获取的方法
        System.out.println("peekFirst = "+list.peekFirst());
        System.out.println("peekFirst获取后  list = "+list);


        System.out.println("================= 移除 =================");
        list.removeFirst();
        list.removeLast();
        System.out.println("removeFirst&removeLast移除后 list = "+list);

        //public E pollFirst()  移除的方法
        System.out.println("pollFirst = "+list.pollFirst());
        System.out.println("pollFirst移除后  list = "+list);

        //public void push(E e)   压栈操作，将元素添加在第一个，等价于addFirst()
        System.out.println("-------------- push ---------------");
        list.push("AAA");
        System.out.println("push添加后  list = "+list);

        //public E pop()          弹栈操作，将第一个元素移除，等价于removeFirst()
        System.out.println("-------------- pop ---------------");
        list.pop();
        System.out.println("pop移除后  list = "+list);
    }

}
